package com.youlu.campus.admin.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.admin.auth.entity.TenantInfo;
import com.youlu.campus.admin.auth.service.TenantInfoService;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.VO.req.TenantInfoQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class TenantInfoServiceImpl implements TenantInfoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean create(TenantInfo tenantInfo) {
        log.info(":>>> 开始创建租户:{}", JSON.toJSONString(tenantInfo));
        tenantInfo.setId(null);
        tenantInfo.setId(null);
        mongoTemplate.insert(tenantInfo);
        return true;
    }

    @Override
    public boolean update(TenantInfo tenantInfo) {
        tenantInfo.setUpdatedTime(new Date());
        mongoTemplate.save(tenantInfo);
        return true;
    }

    @Override
    public boolean delete(String id) {
        if (StringUtils.isBlank(id)) {
            log.error(":>>> 删除租户参数错误");
            throw new BusinessException("删除租户参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        mongoTemplate.remove(query, TenantInfo.class);
        return true;
    }

    @Override
    public Page<TenantInfo> list(TenantInfoQueryVO req) {
        log.info(":>>> 开始查询用户证书订单:{}", JSON.toJSONString(req));
        if (Objects.isNull(req)) {
            log.error(":>> 请求参数错误:{}", JSON.toJSONString(req));
            throw new BusinessException("参数错误");
        }
        Query query = new Query();
        if (StringUtils.isNotBlank(req.getName())) {
            query.addCriteria(Criteria.where("name").is(req.getName()));
        }
        if (Objects.nonNull(req.getStatus())) {
            query.addCriteria(Criteria.where("status").is(req.getStatus()));
        }
        long count = mongoTemplate.count(query, TenantInfo.class);
        PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<TenantInfo> infos = mongoTemplate.find(query.with(pageRequest), TenantInfo.class);
        return PageableExecutionUtils.getPage(infos, pageRequest, () -> count);
    }

    @Override
    public TenantInfo findOne(String id) {
        log.info(":>>> 开始获取详情:{}",id);
        Query query=new Query().addCriteria(Criteria.where("id").is(id));
        mongoTemplate.findOne(query,TenantInfo.class);
        return mongoTemplate.findOne(query,TenantInfo.class);
    }
}
